package io.renren.modules.store.controller;

import java.util.Arrays;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.annotation.NoRepeatSubmit;
import io.renren.common.exception.RRException;
import io.renren.modules.store.entity.TStoreReasonDetailEntity;
import io.renren.modules.store.service.TStoreReasonDetailService;
import io.renren.modules.sys.controller.AbstractController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.store.entity.TStoreReasonEntity;
import io.renren.modules.store.service.TStoreReasonService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 
 *
 * @author steakliu
 * @email 2319492349@qq.com
 * @date 2021-01-25 02:05:20
 */
@RestController
@RequestMapping("store/tstorereason")
public class TStoreReasonController extends AbstractController {
    @Autowired
    private TStoreReasonService tStoreReasonService;
    @Autowired
    private TStoreReasonDetailService storeReasonDetailService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("store:tstorereason:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = tStoreReasonService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{reasonId}")
    @RequiresPermissions("store:tstorereason:info")
    public R info(@PathVariable("reasonId") Long reasonId){
		TStoreReasonEntity tStoreReason = tStoreReasonService.getById(reasonId);

        return R.ok().put("tStoreReason", tStoreReason);
    }

    /**
     * 保存
     */
    @NoRepeatSubmit
    @RequestMapping("/save")
    @RequiresPermissions("store:tstorereason:save")
    public R save(@RequestBody TStoreReasonEntity tStoreReason){
		tStoreReasonService.save(tStoreReason);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("store:tstorereason:update")
    public R update(@RequestBody TStoreReasonEntity tStoreReason){
		tStoreReasonService.updateById(tStoreReason);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("store:tstorereason:delete")
    public R delete(@RequestBody Long[] reasonIds){
        TStoreReasonDetailEntity one = storeReasonDetailService.getOne(new QueryWrapper<TStoreReasonDetailEntity>()
                .eq("seller_id", getSeller())
                .eq("reason_id", reasonIds[0])
        );
        if (one != null){
            throw new RRException("此理由下存在子理由，不能删除");
        }
        tStoreReasonService.removeByIds(Arrays.asList(reasonIds));

        return R.ok();
    }

}
